<template>
	<view class="container padding-top-sm">
		<l-painter custom-style=" left: 74rpx;" isBase64ToPath isRenderImage :board="board" @success="poster = $event">
		</l-painter>
		<button @click="saveImage" class="bg-to-right round padding flex justify-center align-center text-white">
			<image :src="baseUrl + '/static/down3.png'" mode=""></image>
			<view class="margin-left-sm">
				保存本地
			</view>

		</button>
		
	</view>
</template>

<script>
	import lPainter from '@/components/lime-painter/index.vue'
	export default {
		components: {
			lPainter
		},
		data() {
			return {
				baseUrl: this.$config.baseUrl,
				poster: '',
				board: {},
				imageUrl: '',
				page: '',
				scene: ''
			};
		},
		async onLoad() {
			this.scene = `${uni.getStorageSync('userInfo').recommendCode}`
			this.page = 'pages/file/file'
			this.initialization()
			

		},
		methods: {
		
			initialization() {
				const that =this
				this.board = {
					background: '#fff',
					width: '600rpx',
					height: '910rpx',
					views: [{
							type: 'image',
							src: this.baseUrl + '/static/businessschool6.png',
							css: {
								width: '600rpx',
								height: '400rpx',
								top: '0',
								left: '0'
							}
						},
						{
							type: 'view',
							views: [{
									type: 'image',
									src: uni.getStorageSync('userInfo').userIcon,
									css: {
										width: '100rpx',
										height: '100rpx',
										top: '440rpx',
										left: '32rpx',
										radius: '50%'
									}
								},
								{
									type: 'text',
									text: uni.getStorageSync('userInfo').nickname + ' 推荐',
									css: {

										fontSize: '30rpx',
										top: '477rpx',
										left: '156rpx',
										maxLines: 1,
										color: '#666666'
									}
								}
							]
						},
						{
							type: 'view',
							css: {
								background: '#E5E5E5',
								width: '540rpx',
								height: '2rpx',
								top: '581rpx',
								left: '30rpx'
							}

						},
						{
							type: 'image',
							src: this.$api.getWxQRCode + '?scene=' + this.scene + '&page=' + this.page,
							css: {
								width: '197rpx',
								height: '197rpx',
								top: '652rpx',
								left: '200rpx',
								// radius: '50%'
							}
						},
						{
							type: 'text',
							text: '长按扫码进入佐佑帮',
							css: {

								fontSize: '22rpx',
								top: '864rpx',
								left: '202rpx',
								maxLines: 1,
								color: '#666666'
							}
						}
					]
				}
			},
			saveImageToPhotosAlbum() {
				uni.saveImageToPhotosAlbum({
					filePath: this.poster,
					success(res) {
						console.log(res)
						uni.showToast({
							title: '已保存到相册',
							icon: 'success',
							duration: 2000
						})
					}
				})
			},

			saveImage() {
				console.log(this.poster)
				if (!this.poster) return
				// #ifdef  MP-WEIXIN
				uni.getSetting({ //获取用户的当前设置
					success: (res) => {
						if (res.authSetting['scope.writePhotosAlbum']) { //验证用户是否授权可以访问相册
							this.saveImageToPhotosAlbum();
						} else {
							uni.authorize({ //如果没有授权，向用户发起请求
								scope: 'scope.writePhotosAlbum',
								success: () => {
									this.saveImageToPhotosAlbum();
								},
								fail: () => {
									uni.showToast({
										title: "请打开保存相册权限，再点击保存相册分享",
										icon: "none",
										duration: 3000
									});
									setTimeout(() => {
										uni.openSetting({ //调起客户端小程序设置界面,让用户开启访问相册
											success: (res2) => {
												// console.log(res2.authSetting)
											}
										});
									}, 3000);
								}
							})
						}
					}
				})
				// #endif

				// #ifdef  APP-PLUS
				this.saveImageToPhotosAlbum()
				// #endif









			}
		}
	}
</script>

<style lang="scss" scoped>
	button {
		margin: 80rpx 174rpx;

		image {
			width: 44rpx;
			height: 40rpx;
		}
	}
</style>
